<%= render 'title', template: @template %>
<% is_show_tabs = @pagy.pages > 1 || params[:status].present? %>
<% if !@pagy.count.zero? || params[:q].present? %>
  <div class="flex justify-between md:items-end flex-col md:flex-row <%= is_show_tabs ? 'mb-4' : 'mb-6' %>">
    <p class="text-3xl font-bold">Submissions</p>
    <div class="flex justify-end space-x-2 mt-3 md:mt-0">
      <% if params[:q].present? || params[:status].present? || @pagy.pages > 1 %>
        <%= render 'shared/search_input' %>
      <% end %>
      <%= link_to new_template_submissions_export_path(@template), class: 'btn btn-ghost text-base', data: { turbo_frame: 'modal' } do %>
        <%= svg_icon('download', class: 'w-6 h-6 stroke-2') %>
        <span>Export</span>
      <% end %>
      <% if !@template.archived_at? && can?(:create, Submission) %>
        <%= link_to new_template_submission_path(@template), class: 'white-button !border', data: { turbo_frame: 'modal' } do %>
          <%= svg_icon('plus', class: 'w-6 h-6 stroke-2') %>
          <span>Add <span class="hidden md:inline">Recipients</span></span>
        <% end %>
      <% end %>
    </div>
  </div>
<% end %>
<% if is_show_tabs %>
  <div class="flex items-center md:items-end flex-col space-y-2 md:space-y-0 md:flex-row md:space-x-2 mb-4">
    <a href="<%= url_for(params.to_unsafe_h.except(:status)) %>" class="<%= params[:status].blank? ? 'border-neutral-700' : 'border-neutral-300' %> flex h-10 px-2 py-1 text-lg items-center justify-between border text-center text-neutral font-semibold rounded-xl w-full md:w-48 hover:border-neutral-600">
      <div class="flex items-center space-x-1">
        <%= svg_icon('list', class: 'w-5 h-5') %>
        <span class="font-normal">All</span>
      </div>
      <div class="badge badge-neutral badge-outline font-medium">
        <%= params[:status].blank? ? @pagy.count : @base_submissions.count %>
      </div>
    </a>
    <a href="<%= url_for(params.to_unsafe_h.merge(status: :pending)) %>" class="<%= params[:status] == 'pending' ? 'border-neutral-700' : 'border-neutral-300' %> flex h-10 px-2 py-1 text-lg items-center justify-between border text-center text-neutral font-semibold rounded-xl w-full md:w-48 hover:border-neutral-600">
      <div class="flex items-center space-x-1">
        <%= svg_icon('clock', class: 'w-5 h-5') %>
        <span class="font-normal">Pending</span>
      </div>
      <div class="badge badge-neutral badge-outline font-medium">
        <%= params[:status] == 'pending' ? @pagy.count : @base_submissions.pending.count %>
      </div>
    </a>
    <a href="<%= url_for(params.to_unsafe_h.merge(status: :completed)) %>" class="<%= params[:status] == 'completed' ? 'border-neutral-700' : 'border-neutral-300' %> flex h-10 px-2 py-1 text-lg items-center justify-between border text-center text-neutral font-semibold rounded-xl w-full md:w-48 hover:border-neutral-600">
      <div class="flex items-center space-x-1">
        <%= svg_icon('circle_check', class: 'w-5 h-5') %>
        <span class="font-normal">Completed</span>
      </div>
      <div class="badge badge-neutral badge-outline font-medium">
        <%= params[:status] == 'completed' ? @pagy.count : @base_submissions.completed.count %>
      </div>
    </a>
  </div>
<% end %>
<% if @submissions.present? %>
  <div class="space-y-4">
    <%= render partial: 'submission', collection: @submissions %>
  </div>
  <% view_archived_html = capture do %>
    <% if @template.submissions.where.not(archived_at: nil).exists? && !@template.archived_at? %>
      <div>
        <a href="<%= template_archived_index_path(@template) %>" class="link text-sm">View Archived</a>
      </div>
    <% end %>
  <% end %>
  <% if @pagy.pages > 1 %>
    <%= render 'shared/pagination', pagy: @pagy, items_name: 'submissions', left_additional_html: view_archived_html %>
  <% else %>
    <div class="mt-2">
      <%= view_archived_html %>
    </div>
  <% end %>
<% else %>
  <div class="card bg-base-200">
    <div class="card-body text-center py-16">
      <div class="max-w-lg mx-auto">
        <p class="text-3xl font-bold text-base-content mb-4">There are no Submissions</p>
        <% if @template.archived_at.blank? && params[:q].blank? %>
          <p>Send an invitation to fill and complete the form</p>
          <div class="space-y-2 flex flex-col">
            <% if can?(:create, Submission) %>
              <%= link_to new_template_submission_path(@template), class: 'base-button mt-6', data: { turbo_frame: 'modal' } do %>
                <%= svg_icon('plus', class: 'w-6 h-6 stroke-2') %>
                <span class="mr-1">Send to Recipients</span>
              <% end %>
            <% end %>
            <% if @template.submitters.size == 1 %>
              <%= link_to start_form_url(slug: @template.slug), class: 'white-button mt-6', target: '_blank', rel: 'noopener' do %>
                <%= svg_icon('writing', class: 'w-6 h-6') %>
                <span class="mr-1">Sign it Yourself</span>
              <% end %>
            <% else %>
              <%= link_to new_template_submission_path(@template, selfsign: true), class: 'white-button mt-6', data: { turbo_frame: 'modal' } do %>
                <%= svg_icon('writing', class: 'w-6 h-6') %>
                <span class="mr-1">Sign it Yourself</span>
              <% end %>
            <% end %>
          <% end %>
        </div>
      </div>
    </div>
  </div>
<% end %>
